package com.sinosun.autotestmachine.tools.proxy;

import org.json.JSONException;
import org.json.JSONObject;

import com.sinosun.autotestmachine.App;
import com.sinosun.autotestmachine.tools.ValueInfo;
import com.sinosun.autotestmachine.tools.ValueType;
import com.sinosun.autotestmachine.tools.Collector.BaseCollector;
import com.sinosun.autotestmachine.tools.Collector.Cache;
import com.sinosun.autotestmachine.tools.parser.ParamKey;
import com.sinosun.autotestmachine.tools.proxy.callback.CallbackWrapper;
import com.sinosun.autotestmachine.tools.proxy.callback.buddy.BudduyActionType;
import com.sinosun.autotestmachine.tools.proxy.callback.buddy.BuddyActionInformation;
import com.sinosun.autotestmachine.tools.proxy.callback.buddy.BuddyMsg;
import com.sinosun.autotestmachine.tools.utils.Utils;
import com.sinosun.mstplib.MstpClient;
import com.sinosun.mstplib.MstpException;
import com.sinosun.mstplib.buddy.AddPolicy;
import com.sinosun.mstplib.buddy.Buddy;
import com.sinosun.mstplib.buddy.BuddyMember;
import com.sinosun.mstplib.buddy.BuddyPolicy;

import android.text.TextUtils;
import android.util.Log;

public class BuddyModule {
	
	protected static int setAddedPolicy(MstpClient mstpClient, String paramJson) {
		AddPolicy ap = null;
		String question = null;
		String answer = null;
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.POLICY_TYPE)) {
				ap = AddPolicy.valueOf(jo.getInt(ParamKey.POLICY_TYPE));
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			if (jo.has(ParamKey.QUESTION) && !jo.isNull(ParamKey.QUESTION)) {
				question = jo.getString(ParamKey.QUESTION);
			}
			
			if (jo.has(ParamKey.ANSWER) && !jo.isNull(ParamKey.ANSWER)) {
				answer = jo.getString(ParamKey.ANSWER);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		BuddyPolicy bp = new BuddyPolicy();
		bp.setAddPolicy(ap);
		bp.setQuestion(question);
		bp.setAnswer(answer);
		try {
			mstpClient.getBuddyManager().setBuddyPolicy(bp);
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
		
		BuddyPolicy tagBp = new BuddyPolicy();
		try {
			tagBp = mstpClient.getBuddyManager().getAddedPolicy(mstpClient.getMstpId());
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}

		if (bp.getAddPolicy() != tagBp.getAddPolicy()) {
			return ErrorCode.ERR_COMMONT_ERROR;
		}
		
		if (bp.getAddPolicy() == AddPolicy.VERIFY_ANSWER || bp.getAddPolicy() == AddPolicy.VERIFY_ANSWER_AND_COFIRM) {
			if (bp.getQuestion() != null) {
				if (!bp.getQuestion().equals(tagBp.getQuestion())) {
					return ErrorCode.ERR_COMMONT_ERROR;
				}
			} else {
				if (bp.getQuestion() != tagBp.getQuestion()) {
					return ErrorCode.ERR_COMMONT_ERROR;
				}
			}
		}
		
		if (bp.getAddPolicy() == AddPolicy.VERIFY_ANSWER) {
			if (bp.getAnswer() != null) {
				if (!bp.getAnswer().equals(tagBp.getAnswer())) {
					return ErrorCode.ERR_COMMONT_ERROR;
				}
			} else {
				if (bp.getAnswer() != tagBp.getAnswer()) {
					return ErrorCode.ERR_COMMONT_ERROR;
				}
			}
		}
		
		return ErrorCode.SUCCESS;
	}
	
	protected static int getAddedPolicy(MstpClient mstpClient, String paramJson) {
		String invitee = new String();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.INVITEE) && !jo.isNull(ParamKey.INVITEE)) {
				invitee = jo.getString(ParamKey.INVITEE);
				ValueInfo value = Utils.getUserRealInfo(invitee);
				if (value.getValueType() == ValueType.INDEX) {
					invitee = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					invitee = value.getValueContent();
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		try {
			mstpClient.getBuddyManager().getAddedPolicy(invitee);
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
		return ErrorCode.SUCCESS;
	}
	
	protected static int clearAllBuddy(MstpClient mstpClient) {
		try {
			Buddy all = mstpClient.getBuddyManager().getAllBuddy();
			if (all.getBuddyMember() != null) {
				for (BuddyMember member : all.getBuddyMember()) {
					if (!mstpClient.getMstpId().equals(member.getBbuddyMstpId())) {
						mstpClient.getBuddyManager().removeBuddy(member.getBbuddyMstpId());
					}
				}
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return ErrorCode.ERR_COMMONT_ERROR;
		}
		try {
			Buddy tagAll = mstpClient.getBuddyManager().getAllBuddy();
			if (tagAll.getBuddyMember() == null || tagAll.getBuddyMember().size() == 0 || 
					(tagAll.getBuddyMember().size() == 1 && tagAll.getBuddyMember().get(0).getBbuddyMstpId().equals(mstpClient.getMstpId()))) {
				return ErrorCode.SUCCESS;
			}
		} catch (MstpException e) {
			e.printStackTrace();
		}
		return ErrorCode.ERR_COMMONT_ERROR;
	}
	
	protected static int addBuddy(MstpClient mstpClient, String paramJson) {
		String invitee = null;
		String answer = null;
		String comment = null;
		
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.INVITEE)) {
				invitee = jo.getString(ParamKey.INVITEE);
				ValueInfo value = Utils.getUserRealInfo(invitee);
				if (value.getValueType() == ValueType.INDEX) {
					invitee = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					invitee = value.getValueContent();
				}
			}
			if (jo.has(ParamKey.ANSWER) && !jo.isNull(ParamKey.ANSWER)) {
				answer = jo.getString(ParamKey.ANSWER);
			}
			if (jo.has(ParamKey.COMMENT) && !jo.isNull(ParamKey.COMMENT)) {
				comment = jo.getString(ParamKey.COMMENT);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		try {
			int ret = mstpClient.getBuddyManager().addBuddy(invitee, answer, comment);
			if (ret != ErrorCode.SUCCESS) {
				return ret;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
		return ErrorCode.SUCCESS;
	}
	
	protected static int refuseAddBuddy(MstpClient mstpClient, String paramJson) {
		return accessAddBuddy(mstpClient, paramJson, false);
	}
	
	protected static int agreeAddBuddy(MstpClient mstpClient, String paramJson) {
		return accessAddBuddy(mstpClient, paramJson, true);
	}
	
	protected static int setNickName(MstpClient mstpClient, String paramJson) {
		return setInfo(mstpClient, paramJson, 0);
	}
	
	protected static int setExtraInfo(MstpClient mstpClient, String paramJson) {
		return setInfo(mstpClient, paramJson, 1);
	}
	
	private static int setInfo(MstpClient mstpClient, String paramJson, int type) {
		String mstpId = null;
		String nickName = null;
		String extra = null;
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.BUDDY_MSTPID)) {
				mstpId = jo.getString(ParamKey.BUDDY_MSTPID);
				ValueInfo value = Utils.getUserRealInfo(mstpId);
				if (value.getValueType() == ValueType.INDEX) {
					mstpId = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					mstpId = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.NICK_NAME) && !jo.isNull(ParamKey.NICK_NAME)) {
				nickName = jo.getString(ParamKey.NICK_NAME);
			}
			
			if (jo.has(ParamKey.EXTRA_INFO) && !jo.isNull(ParamKey.EXTRA_INFO)) {
				extra = jo.getString(ParamKey.EXTRA_INFO);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		try {
			if (type == 0) {
				int ret = mstpClient.getBuddyManager().setNickName(nickName, mstpId);
				if (ret != ErrorCode.SUCCESS) {
					return ret;
				}
				BuddyMember bm = mstpClient.getBuddyManager().getBuddyInfo(mstpId);
				if (!bm.getNickName().equals(nickName)) {
					return ErrorCode.ERR_COMMONT_ERROR;
				}
				return ErrorCode.SUCCESS;
			} else {
				int ret = mstpClient.getBuddyManager().setExtraInfo(extra, mstpId);
				if (ret != ErrorCode.SUCCESS) {
					return ret;
				}
				BuddyMember bm = mstpClient.getBuddyManager().getBuddyInfo(mstpId);
				if (!bm.getExtraInfo().equals(extra)) {
					return ErrorCode.ERR_COMMONT_ERROR;
				}
				return ErrorCode.SUCCESS;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
	}
	
	protected static int removeBuddy(MstpClient mstpClient, String paramJson) {
		String mstpId = null;
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.BUDDY_MSTPID)) {
				mstpId = jo.getString(ParamKey.BUDDY_MSTPID);
				ValueInfo value = Utils.getUserRealInfo(mstpId);
				if (value.getValueType() == ValueType.INDEX) {
					mstpId = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					mstpId = value.getValueContent();
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		try {
			return mstpClient.getBuddyManager().removeBuddy(mstpId);
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
	}
	
	private static int accessAddBuddy(MstpClient mstpClient, String paramJson, boolean agree) {
		String inviter = null;
		String comment = null;
		
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.INVITER)) {
				inviter = jo.getString(ParamKey.INVITER);
				ValueInfo value = Utils.getUserRealInfo(inviter);
				if (value.getValueType() == ValueType.INDEX) {
					inviter = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					inviter = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.COMMENT) && !jo.isNull(ParamKey.COMMENT)) {
				comment = jo.getString(ParamKey.COMMENT);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		try {
			if (agree) {
				return mstpClient.getBuddyManager().agreeAddBuddy(inviter, comment);
			} else {
				return mstpClient.getBuddyManager().refuseAddBuddy(inviter, comment);
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
	}
	
	protected static int verifyContainBuddy(MstpClient mstpClient, String paramJson) {
		String mstpId = null;
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.BUDDY_MSTPID)) {
				mstpId = jo.getString(ParamKey.BUDDY_MSTPID);
				ValueInfo value = Utils.getUserRealInfo(mstpId);
				if (value.getValueType() == ValueType.INDEX) {
					mstpId = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					mstpId = value.getValueContent();
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		if (TextUtils.isEmpty(mstpId)) {
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		try {
			Buddy all = mstpClient.getBuddyManager().getAllBuddy();
			Log.e("XXX", "size of all = " + all.getBuddyMember().size());
			for (BuddyMember ele : all.getBuddyMember()) {
				if (ele.getBbuddyMstpId().equals(mstpId)) {
					return ErrorCode.SUCCESS;
				}
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
		return ErrorCode.ERR_COMMONT_ERROR;
	}
	
	protected static int reciveAddBuddyNotifyAndVerify(MstpClient mstpClient, String paramJson) {
		String inviter = null;
		String invitee = null;
		String comment = new String("");
		
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.INVITER)) {
				inviter = jo.getString(ParamKey.INVITER);
				ValueInfo value = Utils.getUserRealInfo(inviter);
				if (value.getValueType() == ValueType.INDEX) {
					inviter = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					inviter = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.INVITEE)) {
				invitee = jo.getString(ParamKey.INVITEE);
				ValueInfo value = Utils.getUserRealInfo(invitee);
				if (value.getValueType() == ValueType.INDEX) {
					invitee = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					invitee = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.COMMENT) && !jo.isNull(ParamKey.COMMENT)) {
				comment = jo.getString(ParamKey.COMMENT);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		BaseCollector collector = new BaseCollector();
		collector.setResult(ErrorCode.ERR_TIMEOUT_ERROR);
		BuddyActionInformation item = new BuddyActionInformation();
		item.setBudduyActionType(BudduyActionType.AddBuddy);
		BuddyMsg info = new BuddyMsg();
		Log.e("XXX", "inviter " + inviter);
		Log.e("XXX", "invitee " + invitee);
		Log.e("XXX", "comment " + comment);
		info.setArg(1, inviter);
		info.setArg(2, invitee);
		info.setArg(3, comment);
		item.setBuddyMsg(info);
		CallbackWrapper.makeBuddyListener(collector, item);
		Cache.allBuddyAction.clear();
		try {
			collector.waitResult(15);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return ErrorCode.ERR_COMMONT_ERROR;
		}
		return collector.getResult();
	}
	
	protected static int reciveAgreeAddBuddyNotifyAndVerify(MstpClient mstpClient, String paramJson) {
		String invitee = null;
		String inviter = null;
		String comment = new String("");
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.INVITEE)) {
				invitee = jo.getString(ParamKey.INVITEE);
				ValueInfo value = Utils.getUserRealInfo(invitee);
				if (value.getValueType() == ValueType.INDEX) {
					invitee = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					invitee = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.INVITER)) {
				inviter = jo.getString(ParamKey.INVITER);
				ValueInfo value = Utils.getUserRealInfo(inviter);
				if (value.getValueType() == ValueType.INDEX) {
					inviter = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					inviter = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.COMMENT) && !jo.isNull(ParamKey.COMMENT)) {
				comment = jo.getString(ParamKey.COMMENT);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		BaseCollector collector = new BaseCollector();
		collector.setResult(ErrorCode.ERR_TIMEOUT_ERROR);
		BuddyActionInformation item = new BuddyActionInformation();
		item.setBudduyActionType(BudduyActionType.AgreeAddBuddy);
		BuddyMsg info = new BuddyMsg();
		info.setArg(1, inviter);
		info.setArg(2, invitee);
		info.setArg(3, comment);
		item.setBuddyMsg(info);
		CallbackWrapper.makeBuddyListener(collector, item);
		Cache.allBuddyAction.clear();
		try {
			collector.waitResult(15);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return ErrorCode.ERR_COMMONT_ERROR;
		}
		return collector.getResult();
	}
	
	protected static int reciveRefuseAddBuddyNotifyAndVerify(MstpClient mstpClient, String paramJson) {
		String invitee = null;
		String comment = null;
		String inviter = null;
		try {
			JSONObject jo;
			jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.INVITEE)) {
				invitee = jo.getString(ParamKey.INVITEE);
				ValueInfo value = Utils.getUserRealInfo(invitee);
				if (value.getValueType() == ValueType.INDEX) {
					invitee = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					invitee = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.INVITER)) {
				inviter = jo.getString(ParamKey.INVITER);
				ValueInfo value = Utils.getUserRealInfo(inviter);
				if (value.getValueType() == ValueType.INDEX) {
					inviter = App.getTestConfig().getUsers().get(value.getValueContent());
				} else {
					inviter = value.getValueContent();
				}
			}
			
			if (jo.has(ParamKey.COMMENT) && !jo.isNull(ParamKey.COMMENT)) {
				comment = jo.getString(ParamKey.COMMENT);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		BaseCollector collector = new BaseCollector();
		collector.setResult(ErrorCode.ERR_TIMEOUT_ERROR);
		BuddyActionInformation item = new BuddyActionInformation();
		item.setBudduyActionType(BudduyActionType.RefuseAddBuddy);
		BuddyMsg info = new BuddyMsg();
		info.setArg(1, inviter);
		info.setArg(2, invitee);
		info.setArg(3, comment);
		item.setBuddyMsg(info);
		Log.e("XXX", "item => " + item.toString());
		CallbackWrapper.makeBuddyListener(collector, item);
		Cache.allBuddyAction.clear();
		try {
			collector.waitResult(15);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return ErrorCode.ERR_COMMONT_ERROR;
		}
		return collector.getResult();
	}
}
